<template>
  <view class="search-container">
    <!-- 搜索框区域 -->
    <view class="search-input-wrapper">
      <image
          src="/static/images/search-icon.png"
          class="search-icon"
          mode="aspectFit"
      />
      <input
          v-model="searchValue"
          class="search-input"
          :placeholder="placeholder"
          placeholder-style="color: #999;"
          confirm-type="search"
          @confirm="handleSearch"
      />
      <image
          v-if="searchValue"
          src="/static/images/clear-icon.png"
          class="clear-icon"
          @click="searchValue = ''"
      />
    </view>

    <!-- 搜索按钮 -->
    <my-button @click="handleSearch"> 搜索</my-button>
  </view>
</template>

<script>

import MyButton from "../button/myButton.vue";

export default {
  name: "SearchBox",
  components: {MyButton},
  props: {
    placeholder: String,
    isRefresh: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      searchValue: ""
    };
  },
  watch: {
    isRefresh: {
      handler(newVal) {
        if (newVal) {
          this.searchValue = "";
        }
      },
      deep: true,
      immediate: true,
    }
  },
  methods: {
    handleSearch() {
      this.$emit("search", this.searchValue.trim());
      this.isRefresh = false
    }
  }
};
</script>

<style lang="scss" scoped>
.search-container {
  display: flex;
  align-items: center;
  padding: 10rpx;
  background: #fff;
}

.search-input-wrapper {
  flex: 1;
  position: relative;
  background: #f5f5f5;
  border-radius: 40rpx;
  padding: 0 80rpx;

  .search-icon {
    position: absolute;
    left: 20rpx;
    top: 50%;
    transform: translateY(-50%);
    width: 36rpx;
    height: 36rpx;
  }

  .clear-icon {
    position: absolute;
    right: 20rpx;
    top: 50%;
    transform: translateY(-50%);
    width: 32rpx;
    height: 32rpx;
  }

  .search-input {
    height: 70rpx;
    font-size: 28rpx;
    color: #333;
  }
}
</style>
